<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<!DOCTYPE html>
<html>
<head>
<title>产品管理</title>
<jsp:include page="../inc.jsp"></jsp:include>
<script type="text/javascript">
	var dataGrid;
	var editRow = undefined;

	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			url : '${pageContext.request.contextPath}/productController/dataGrid',
			fit : true,
			fitColumns : true,
			border : false,
			pagination : true,
			idField : 'id',
			pageSize : 20,
			pageList : [ 20, 30, 40, 50 ],
			sortName : 'name',
			sortOrder : 'desc',
			checkOnSelect : true,
			selectOnCheck : true,
			nowrap : false,
			striped : true,
			rownumbers : true,
			//singleSelect : true,
			frozenColumns : [ [ {
				field : 'id',
				title : '编号',
				width : 150,
				//hidden : true,
				checkbox : true
			}, {
				field : 'name',
				title : '产品名称',
				width : 200,
				sortable : true,
				editor : {
					type : 'validatebox',
					options : {
						required : true
					}
				}
			} ] ],
			columns : [ [ {
				field : 'description',
				title : '产品描述',
				width : 200,
				editor : {
					type : 'validatebox',
					options : {
						required : true
					}
				}
			} ] ],
			toolbar : [ {
				text : '增加',
				iconCls : 'pencil_add',
				handler : function() {
					if (editRow != undefined) {
						dataGrid.datagrid('endEdit', editRow);
					}
					if (editRow == undefined) {
						dataGrid.datagrid('insertRow', {
							index : 0, // index start with 0
							row : {
								name : '请输入产品名称',
								description : '请输入产品描述'
							}
						});
						dataGrid.datagrid('beginEdit', 0);
						editRow = 0;
					}
				}
			}, '-', {
				text : '修改',
				iconCls : 'pencil',
				handler : function() {
					var rows = dataGrid.datagrid('getSelections');
					if (rows.length == 1) {
						if (editRow != undefined) {
							dataGrid.datagrid('endEdit', editRow);
						}
						if (editRow == undefined) {
							var index = dataGrid.datagrid('getRowIndex', rows[0]);
							dataGrid.datagrid('beginEdit', index);
							editRow = index;
							dataGrid.datagrid('unselectAll');
						}
					} else {
						$.messager.show({
							title : '提示',
							msg : '请选择一条记录!',
							timeout : 5000,
							showType : 'slide'
						});
					}

				}
			}, '-', {
				text : '删除',
				iconCls : 'pencil_delete',
				handler : function() {
					var rows = dataGrid.datagrid('getSelections');
					if (rows.length > 0) {
						$.messager.confirm('请确认', '您确定要删除当前所有选择的记录吗？', function(b) {
							if (b) {
								var ids = [];
								for(var i=0;i<rows.length;i++){
									ids.push(rows[i].id)
								}
								$.ajax({
									url : '${pageContext.request.contextPath}/productController/delete',
									data : {
										ids : ids.join(',')
									},
									dataType : 'json',
									success : function(r) {
										if (r && r.success) {
											dataGrid.datagrid('load');
											dataGrid.datagrid('unselectAll');
											$.messager.show({
												msg : r.msg,
												title : '成功'
											});
											dataGrid.datagrid('acceptChanges');
										} else {
											$.messager.alert('失败', r.msg, 'error');
											dataGrid.datagrid('rejectChanges');
										}
									}
								});
							}
						});
					} else {
						$.messager.alert('提示', '请选择要删除的记录!', 'warning');
					}
				}
			}, '-', {
				text : '保存',
				iconCls : 'disk',
				handler : function() {
					dataGrid.datagrid('endEdit', editRow);
				}
			}, '-', {
				text : '取消编辑',
				iconCls : 'cancel',
				handler : function() {
					dataGrid.datagrid('rejectChanges');
					dataGrid.datagrid('unselectAll');
					editRow = undefined;
				}
			}, '-' ],
			onLoadSuccess : function() {
				parent.$.messager.progress('close');
				//$(this).datagrid('tooltip');
			},
			onAfterEdit : function(rowIndex, rowData, changes) {
				var inserted = dataGrid.datagrid('getChanges', 'inserted');
				var updated = dataGrid.datagrid('getChanges', 'updated');
				var url = '';
				var operationType = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/productController/add'
					operationType = '添加'
				}else{
					if (updated.length > 0) {
						url = '${pageContext.request.contextPath}/productController/edit'
						operationType = '修改'
					}else{
						$.messager.alert('注意', '请修改记录', 'error');
						// 当前编辑索引取消
						editRow = undefined;
						return;
					}
				}
				
				$.ajax({
					url : url,
					data : rowData,
					dataType : 'json',
					success : function(r) {
						if (r && r.success) {
							dataGrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : operationType+'成功'
							});
							dataGrid.datagrid('load');
						} else {
							dataGrid.datagrid('rejectChanges');
							$.messager.alert(operationType+'失败', r.msg, 'error');
						}
						// 当前编辑索引取消
						editRow = undefined;
						dataGrid.datagrid('unselectAll');
					}
				});

			},
			onDblClickRow : function(rowIndex, rowData) {
				if (editRow != undefined) {
					dataGrid.datagrid('endEdit', editRow);
				}
				if (editRow == undefined) {
					dataGrid.datagrid('beginEdit', rowIndex);
					editRow = rowIndex;
				}

			}

		});

	});

</script>
</head>
<body>
	<div id="searchFormDiv" class="easyui-layout"
		data-options="fit : true,border : false">
		<div data-options="region:'center',border:false">
			<table id="dataGrid"></table>
		</div>
	</div>
</body>
</html>